:- module(t_record,[t_record/2]).

/**
 * Handles the conversion between Dictionary data
 * and RDF
 */

:- use_module(library('semweb/rdf_db')).


%perhaps worth pooling table handles


t_record(Id,List) :-
	length(List,N),
	handle_table(Id,Handle,N),
	column_pattern(N,Pat),
	in_table(Handle,Pat,_),
	clean(Pat,List).


:- dynamic table/3.
handle_table(File,Handle,N) :-
	table(File,Handle,N),!.
handle_table(File,Handle,N) :-
	column_spec(N,Columns),
	new_table(File,Columns,[field_separator(9)],Handle),
	assert(table(File,Handle,N)).


clean([],[]).
clean([H|T],[H2|T2]) :-
	arg(1,H,H2),
	clean(T,T2).

column_spec(Length,Spec) :-
	columns(Length,Cols),
	to_spec(Cols,Spec).
column_pattern(Length,Pattern) :-
	columns(Length,Cols),
	to_pattern(Cols,Pattern).
columns(Length,Cols) :-
	findall(T,(between(1,Length,N),atom_concat(c,N,T)),Cols).


to_spec([],[]).
to_spec([H|T],[H2|T2]) :-
	H2=..[H,atom], %this may be not the right way...
	to_spec(T,T2).
to_pattern([],[]).
to_pattern([H|T],[H2|T2]) :-
	H2=..[H,_],
	to_pattern(T,T2).


